<template>
  <el-dialog
    :title="!dataForm.wmno ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="序号" prop="serialnumber">
      <el-input v-model="dataForm.serialnumber" placeholder="序号"></el-input>
    </el-form-item>
    <el-form-item label="设备名称" prop="wname">
      <el-input v-model="dataForm.wname" placeholder="设备名称"></el-input>
    </el-form-item>
    <el-form-item label="工件编号" prop="gjnumber">
      <el-input v-model="dataForm.gjnumber" placeholder="工件编号"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="starttime">
      <el-input v-model="dataForm.starttime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endtime">
      <el-input v-model="dataForm.endtime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="时长" prop="duration">
      <el-input v-model="dataForm.duration" placeholder="时长"></el-input>
    </el-form-item>
    <el-form-item label="良好" prop="lh">
      <el-input v-model="dataForm.lh" placeholder="良好"></el-input>
    </el-form-item>
    <el-form-item label="正常" prop="norm">
      <el-input v-model="dataForm.norm" placeholder="正常"></el-input>
    </el-form-item>
    <el-form-item label="报警" prop="alarm">
      <el-input v-model="dataForm.alarm" placeholder="报警"></el-input>
    </el-form-item>
    <el-form-item label="规范符合率" prop="normaccord">
      <el-input v-model="dataForm.normaccord" placeholder="规范符合率"></el-input>
    </el-form-item>
    <el-form-item label="电流均值" prop="midcur">
      <el-input v-model="dataForm.midcur" placeholder="电流均值"></el-input>
    </el-form-item>
    <el-form-item label="电流最大值" prop="maxcur">
      <el-input v-model="dataForm.maxcur" placeholder="电流最大值"></el-input>
    </el-form-item>
    <el-form-item label="电压均值" prop="midvol">
      <el-input v-model="dataForm.midvol" placeholder="电压均值"></el-input>
    </el-form-item>
    <el-form-item label="电压最大值" prop="maxvol">
      <el-input v-model="dataForm.maxvol" placeholder="电压最大值"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          wmno: 0,
          serialnumber: '',
          wname: '',
          gjnumber: '',
          starttime: '',
          endtime: '',
          duration: '',
          lh: '',
          norm: '',
          alarm: '',
          normaccord: '',
          midcur: '',
          maxcur: '',
          midvol: '',
          maxvol: ''
        },
        dataRule: {
          serialnumber: [
            { required: true, message: '序号不能为空', trigger: 'blur' }
          ],
          wname: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],
          gjnumber: [
            { required: true, message: '工件编号不能为空', trigger: 'blur' }
          ],
          starttime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endtime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          duration: [
            { required: true, message: '时长不能为空', trigger: 'blur' }
          ],
          lh: [
            { required: true, message: '良好不能为空', trigger: 'blur' }
          ],
          norm: [
            { required: true, message: '正常不能为空', trigger: 'blur' }
          ],
          alarm: [
            { required: true, message: '报警不能为空', trigger: 'blur' }
          ],
          normaccord: [
            { required: true, message: '规范符合率不能为空', trigger: 'blur' }
          ],
          midcur: [
            { required: true, message: '电流均值不能为空', trigger: 'blur' }
          ],
          maxcur: [
            { required: true, message: '电流最大值不能为空', trigger: 'blur' }
          ],
          midvol: [
            { required: true, message: '电压均值不能为空', trigger: 'blur' }
          ],
          maxvol: [
            { required: true, message: '电压最大值不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.wmno = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.wmno) {
            this.$http({
              url: this.$http.adornUrl(`/wmhistory/wmhistory/info/${this.dataForm.wmno}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.serialnumber = data.wmhistory.serialnumber
                this.dataForm.wname = data.wmhistory.wname
                this.dataForm.gjnumber = data.wmhistory.gjnumber
                this.dataForm.starttime = data.wmhistory.starttime
                this.dataForm.endtime = data.wmhistory.endtime
                this.dataForm.duration = data.wmhistory.duration
                this.dataForm.lh = data.wmhistory.lh
                this.dataForm.norm = data.wmhistory.norm
                this.dataForm.alarm = data.wmhistory.alarm
                this.dataForm.normaccord = data.wmhistory.normaccord
                this.dataForm.midcur = data.wmhistory.midcur
                this.dataForm.maxcur = data.wmhistory.maxcur
                this.dataForm.midvol = data.wmhistory.midvol
                this.dataForm.maxvol = data.wmhistory.maxvol
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/wmhistory/wmhistory/${!this.dataForm.wmno ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'wmno': this.dataForm.wmno || undefined,
                'serialnumber': this.dataForm.serialnumber,
                'wname': this.dataForm.wname,
                'gjnumber': this.dataForm.gjnumber,
                'starttime': this.dataForm.starttime,
                'endtime': this.dataForm.endtime,
                'duration': this.dataForm.duration,
                'lh': this.dataForm.lh,
                'norm': this.dataForm.norm,
                'alarm': this.dataForm.alarm,
                'normaccord': this.dataForm.normaccord,
                'midcur': this.dataForm.midcur,
                'maxcur': this.dataForm.maxcur,
                'midvol': this.dataForm.midvol,
                'maxvol': this.dataForm.maxvol
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
